﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using VideoClassLibrary.BusinessLogicLayer;
using VideoClassLibrary.Model;

/// <summary>
/// Summary description for UserBase
/// </summary>
public class UserBase : System.Web.UI.Page
{
    /// <summary>
    /// 未登录跳转和界面级权限识别
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void UserBase_Load(object sender, EventArgs e)
    {
        if (System.Web.HttpContext.Current.Session["currentUser"] == null)
        {

            string schoolId = HttpContext.Current.Request.QueryString["SchoolId"];

            Response.Redirect("~/UserLogin.aspx?returnUrl=" + this.ReturnBackURL+"&SchoolId="+schoolId, true);
            return;
            //window.location.target='_perant';

            //Response.Write("<script>top.location.href='/login.aspx';</script>");
            //Response.End();
        }

    
    }

    public virtual String ReturnBackURL
    {
        get
        {
            
            if (ViewState["__ReturnBackURL__"] == null)
            {
                string schoolId = HttpContext.Current.Request.QueryString["SchoolId"];
                ViewState["__ReturnBackURL__"] = "UserPersonalCenter/UserCourseCalendar.aspx?SchoolId=" + schoolId;
            }
            return Server.UrlEncode(ViewState["__ReturnBackURL__"].ToString());
        }
        set { ViewState["__ReturnBackURL__"] = value; }
    }

    public UserBase()
    {

    }

    protected override void OnInit(EventArgs e)
    {

        this.Load += new EventHandler(UserBase_Load);

        base.OnInit(e);

    }

    protected override void OnLoad(EventArgs e)
    {
        string schoolId = HttpContext.Current.Request.QueryString["SchoolId"];
        if (Session["currentUser"] != null)
        {
            base.OnLoad(e);
        }
        else
        {
            
            Response.Redirect("~/UserLogin.aspx?returnUrl=" + this.ReturnBackURL + "&SchoolId="+schoolId, true);
        }
    }

    public V_user CurrentUser
    {
        get
        {
            if (Session["currentUser"] == null)
            {
                string schoolId = HttpContext.Current.Request.QueryString["SchoolId"];
                Response.Redirect("~/UserLogin.aspx?returnUrl=" + this.ReturnBackURL+ "&SchoolId="+schoolId, true);
            }

            return (V_user)Session["currentUser"];
        }
        set { Session["currentUser"] = value; }
    }


    public int UserID
    {
        get
        {
            return CurrentUser.ID;
        }
    }

    public int SchoolID
    {
        get
        {
            return CurrentUser.SchoolID;
        }
    }

    public string LoginName
    {
        get
        {
            return CurrentUser.VU_loginname;
        }
    }
    public string RealName
    {
        get
        {
            return CurrentUser.VU_realname;
        }
    }
    public int RoleID
    {
        get
        {
            return 1;
            //return CurrentUser.RoleID;
        }
    }
   
}
